package net.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bo.KUtilisateur;
import net.ko.framework.KoSession;
import net.ko.http.objects.KRequest;
import net.ko.internet.KEmail;
import net.ko.internet.KServer;

/**
 * Servlet implementation class SSendEmailForgetPassword
 */
@WebServlet("/SSendEmailForgetPassword.act")
public class SSendEmailForgetPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SSendEmailForgetPassword() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		String to = KRequest.GETPOST("mail", request);

		KUtilisateur user = KoSession.kloadOne(KUtilisateur.class, "mail='" + to + "'");

		String login = user.getLogin();
		String password = user.getPassword();

		KEmail email = new KEmail();
		KServer server = new KServer("smtp.orange.fr", "", "");
		server.setAuthentification(true);
		email.setMailServer(server);
		email.createMessage();

		email.setTo(to);
		email.setFrom("plan2tests@support.fr");
		email.setSubject("Identifiants");
		email.setText(""
				+ "<div style='background: #d5cea6;"
				+ "background: -moz-linear-gradient(top,  #d5cea6 0%, #c9c190 40%, #b7ad70 100%);"
				+ "background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#d5cea6), color-stop(40%,#c9c190), color-stop(100%,#b7ad70));"
				+ "background: -webkit-linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
				+ "background: -o-linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
				+ "background: -ms-linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
				+ "background: linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
				+ "color:#fff;"
				+ "padding:20px;"
				+ "-webkit-border-radius: 10px 10px 10px 10px;"
				+ "-moz-border-radius: 10px 10px 10px 10px;"
				+ "border-radius:  10px 10px 10px 10px;"
				+ "font-size:15px;'>"
				+ ""
				+ "<img src='http://tests.kobject.net/images/main/logo-kobject-tests-fonctionnels.png'>"
				+ "<br><br>"
				+ "La demande de mot de passe a aboutie"
				+ "<br>"
				+ "Voici ci-dessous vos identifiants de connexion : "
				+ "<br><br>"
				+ "Identifiant : " + login
				+ "<br>"
				+ "Mot de passe : " + password
				+ "<br><br>"
				+ "<span style='font-size:11px'>Cet email a été envoyé automatiquement merci de ne pas répondre</span>"
				+ "</div>");

		email.send(true);

		out.print("<div class='validate'>Votre demande a bien été prise en compte</div> <br> Un email récapitulatif vous a été envoyé à l'adresse suivante : <b>" + to + "</b> <br>Vous pouvez désormais vous connecter à l'aide de vos identifiants de connexion");
	}

}
